package org.fao.pims.web.manage.book;


import javax.servlet.http.*;
import java.util.*;
import org.apache.commons.logging.*;
import org.apache.struts.action.*;
import org.fao.pims.web.manage.book.BookForm;
import org.fao.pims.service.manage.book.*;
import org.fao.pims.model.*;

public class BookAction extends Action {
	
	private static Log log = LogFactory.getLog(BookAction.class);
	
	public ActionForward execute(
			ActionMapping mapping,
			ActionForm form,
			HttpServletRequest request,
			HttpServletResponse response) throws Exception
	{
		
		BookForm bookForm = (BookForm) form;
		
		String scientificName = bookForm.getScientificName();
		
		System.out.println("Scientific name for search in documents: " + scientificName);
		log.info("Scientific name for search in documents: " + scientificName);
		
		BookManager manager = new BookManagerImpl();
		
		try {
			
			List bookList = manager.getBookChapters(scientificName);
			
			request.setAttribute("chapters", bookList);
			
			return mapping.findForward("success");
						
			
		}
		catch(BookException be) {
			
			System.out.println(be.getMessage());
			
			ActionMessages errors = new ActionMessages();
			
			errors.add(
                    ActionMessages.GLOBAL_MESSAGE,
                    new ActionMessage("book.search.exception"));
			
			this.saveErrors(request, errors);
			
			return mapping.findForward("failure");
			
			
		}
	}

}
